package com.jyy.prefabricated.service;

import com.jyy.prefabricated.entity.MessageInfo;
import com.jyy.prefabricated.entity.RecruitInfo;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.entity.TrainingOrganization;
import com.jyy.prefabricated.enums.ApplyStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.RecruitInfoMapper;
import com.jyy.prefabricated.mapper.TrainingOrganizationMapper;
import com.jyy.prefabricated.vo.RecruitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */
@Transactional
@Service
public class RecruitInfoService {

    @Autowired
    private RecruitInfoMapper recruitInfoMapper;

    @Autowired
    private TrainingOrganizationMapper trainingOrganizationMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private MessageInfoService messageInfoService;

    /**
     * 新增
     * @param recruitInfo
     * @return
     */
    public Integer insert(RecruitInfo recruitInfo){
        recruitInfoMapper.insert(recruitInfo);
        return recruitInfo.getId();
    }

    /**
     * 更新
     * @param recruitInfo
     * @return
     */
    public int update(RecruitInfo recruitInfo){
        if(recruitInfo.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return recruitInfoMapper.updateByPrimaryKeySelective(recruitInfo);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return recruitInfoMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public RecruitInfo selectById(Integer id){
        return recruitInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过
     * @param id
     */
    public void apply(Integer id, SysUser user){
        RecruitInfo recruitInfo = recruitInfoMapper.selectByPrimaryKey(id);
        recruitInfo.setApplyUserId(user.getId());
        recruitInfo.setApplyStatus(ApplyStatusEnum.SUCCESS.getValue());
        TrainingOrganization trainingOrganization = trainingOrganizationMapper.selectByPrimaryKey(recruitInfo.getTrainingOrganizationId());
        SysUser u = sysUserService.selectByUsername(trainingOrganization.getAccount());
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setUserId(u.getId());
        messageInfo.setContent(recruitInfo.getPosition() + "审核通过");
        messageInfoService.insert(messageInfo);
        update(recruitInfo);
    }

    /**
     * 驳回
     * @param id
     */
    public void reject(Integer id, SysUser user){
        RecruitInfo recruitInfo = recruitInfoMapper.selectByPrimaryKey(id);
        recruitInfo.setApplyUserId(user.getId());
        recruitInfo.setApplyStatus(ApplyStatusEnum.FAIL.getValue());
        TrainingOrganization trainingOrganization = trainingOrganizationMapper.selectByPrimaryKey(recruitInfo.getTrainingOrganizationId());
        SysUser u = sysUserService.selectByUsername(trainingOrganization.getAccount());
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setUserId(u.getId());
        messageInfo.setContent(recruitInfo.getPosition() + "审核未通过");
        messageInfoService.insert(messageInfo);
        update(recruitInfo);
    }

    /**
     * 后台查询接口
     * @param keyword
     * @param trainingOrganizationId
     * @param applyStatus
     * @return
     */
    public List<RecruitInfo> selectByParams(String keyword,String startDate,String endDate,Integer trainingOrganizationId, Integer applyStatus){
        return recruitInfoMapper.selectByParams(keyword, startDate, endDate, trainingOrganizationId,applyStatus);
    }

    /**
     * 通过关键字查询招聘信息
     * @param keyword
     * @return
     */
    public List<RecruitVo> selectRecruitVoByName(String keyword){
        return recruitInfoMapper.selectRecruitVoByName(keyword);
    }

    /**
     * 查询机构招聘信息审核日志
     * @param trainingOrganizationId 机构id
     * @return
     */
    public List<RecruitInfo> selectApplyLog(Integer trainingOrganizationId){
        return recruitInfoMapper.selectApplyLog(trainingOrganizationId);
    }

    /**
     * 查询热门招聘信息
     * @return
     */
    public List<RecruitVo> selectByHot(){
        return recruitInfoMapper.selectByHot();
    }
}
